<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '140px'"
    >
      <!-- <el-form-item label="ID" prop="id">
        <el-input v-model="dataForm.id" placeholder="ID"></el-input>
      </el-form-item> -->
      <el-row>
        <el-col :span="12">
          <el-form-item label="短信编码" prop="smsCode">
            <el-select
              v-model="dataForm.smsCode"
              clearable
              placeholder="短信编码"
            >
              <el-option
                v-for="item in smsCodeList"
                :key="item.smsCode"
                :value="item.smsCode"
                :label="item.smsCode"
              ></el-option
            ></el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="短信类型" prop="templateType">
            <SelectComponent
              ref="templateType"
              dictType="sms_type"
              v-model="dataForm.templateType"
              placeholder="短信类型"
            >
            </SelectComponent>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="21">
          <el-form-item label="模板名称" prop="templateName">
            <el-input
              v-model="dataForm.templateName"
              placeholder="模板名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"> </el-col>
      </el-row>
      <el-row>
        <el-col :span="21">
          <el-form-item label="模板内容" prop="templateContent">
            <el-input
              v-model="dataForm.templateContent"
              placeholder="模板内容"
              type="textarea"
              :rows="4"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- <el-form-item label="模板编码" prop="templateCode">
        <el-input
          v-model="dataForm.templateCode"
          placeholder="模板编码"
        ></el-input>
      </el-form-item> -->
      <el-row>
        <el-col :span="21">
          <el-form-item label="短信模板申请说明" prop="templateRemark">
            <el-input
              v-model="dataForm.templateRemark"
              placeholder="短信模板申请说明"
              type="textarea"
              :rows="4"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12"> </el-col>
      </el-row>
      <!-- <el-form-item
        label="'申请状态  0：审核中。1：审核通过。2：审核未通过，请在返回参数Reason中查看审核失败原因。10：取消审核。'"
        prop="templateStatus"
      >
        <el-input
          v-model="dataForm.templateStatus"
          placeholder="'申请状态  0：审核中。1：审核通过。2：审核未通过，请在返回参数Reason中查看审核失败原因。10：取消审核。'"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="审核备注" prop="templateReason">
        <el-input
          v-model="dataForm.templateReason"
          placeholder="审核备注"
        ></el-input>
      </el-form-item> -->
      <!-- <el-form-item label="'状态  0：停用    1：正常'" prop="status">
              <el-input v-model="dataForm.status" placeholder="'状态  0：停用    1：正常'"></el-input>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
            </el-form-item>
            <el-form-item label="'删除标识  0：未删除    1：删除'" prop="delFlag">
              <el-input v-model="dataForm.delFlag" placeholder="'删除标识  0：未删除    1：删除'"></el-input>
            </el-form-item>
            <el-form-item label="创建者" prop="creator">
              <el-input v-model="dataForm.creator" placeholder="创建者"></el-input>
            </el-form-item>
            <el-form-item label="创建时间" prop="createDate">
              <el-input v-model="dataForm.createDate" placeholder="创建时间"></el-input>
            </el-form-item>
            <el-form-item label="更新者" prop="updater">
              <el-input v-model="dataForm.updater" placeholder="更新者"></el-input>
            </el-form-item>
            <el-form-item label="更新时间" prop="updateDate">
              <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>
            </el-form-item> -->
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        smsCode: "",
        templateType: "1",
        templateName: "",
        templateCode: "",
        templateContent: "",
        templateRemark: "",
        templateStatus: "",
        templateReason: "",
        status: "",
        remark: "",
        delFlag: "",
        creator: "",
        createDate: "",
        updater: "",
        updateDate: "",
      },
      smsCodeList: [],
    };
  },
  created() {
    this.getSmsServe();
  },

  mounted() {
    // this.$nextTick(() => {
    //   this.$refs.templateType.selectValue = this.dataForm.templateType;
    // });
  },
  computed: {
    dataRule() {
      return {
        smsCode: [
          { required: true, message: "请输入短信编码", trigger: "blur" },
        ],
        templateType: [
          { required: true, message: "请输入短信类型", trigger: "blur" },
        ],
        templateName: [
          { required: true, message: "请输入模板名称", trigger: "blur" },
        ],
        templateContent: [
          { required: true, message: "请输入模板内容", trigger: "blur" },
        ],
        templateRemark: [
          {
            required: true,
            message: "请输入短信模板申请说明",
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    getSmsServe() {
      this.$http.get("/message/sms/list").then((res) => {
        if (res.data.msg == "success") {
          this.smsCodeList = res.data.data;
        }
      });
    },
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get("/message/issmstemplate/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
           this.$refs.templateType.selectValue=this.dataForm.templateType
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/message/issmstemplate/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
